//
//  AccountVC.swift
//  ImGuider X
//
//  Created by 王鹏宇 on 9/26/18.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

class AccountVC: BaseVC {
    
    @IBOutlet weak var tableView: UITableView!
    
    @IBOutlet weak var languageLabel: UILabel!
    @IBOutlet weak var aboutLabel: UILabel!
    @IBOutlet weak var shareLabel: UILabel!
    
    @IBOutlet weak var logoutBtn: UIButton!
    
    @IBOutlet weak var languageView: UIView!
    
    @IBOutlet weak var aboutView: UIView!
    
    @IBOutlet weak var shareView: UIView!
    
    private var dataSource:[[String:String]] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        setupData()
    }
    
    private func setupUI() {
        
        tableView.register(UINib(nibName: "AccountCell", bundle: nil), forCellReuseIdentifier: "AccountCell")
        tableView.estimatedRowHeight = 100
        tableView.rowHeight = UITableView.automaticDimension
        tableView.backgroundColor = kThemeBgColor
        
        
        logoutBtn.setTitle(LocalizedString("logout"), for: .normal)
        languageLabel.text = LocalizedString("language_setting")
        aboutLabel.text = LocalizedString("about_company")
        
        setupHeadView()
        
        let colors = [UIColor.hexStringToColor(hexString: "#A8A8A8"),UIColor.hexStringToColor(hexString: "#505050")]
        let layer = CAGradientLayer.gradientLayer(colors: colors, position:.topToBottom, frame: CGRect(x: 0, y: 0, width: kScreenWidth - 32, height: 50))
        layer.cornerRadius = 10
        
        logoutBtn.layer.insertSublayer(layer, at: 0)
        
        languageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(languageAction)))
        aboutView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(aboutusAction)))
        shareView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(shareAction)))
    }
    
    @objc private func languageAction() {
        
        let lanVC = LanguageSettingVC()
        
        self.navigationController?.pushViewController(lanVC, animated: true)
    }
    
    @objc private func aboutusAction() {
        
        let aboutVC = AboutVC()
        
        self.navigationController?.pushViewController(aboutVC, animated: true)
    }
    @objc private func shareAction() {
        
        self.view.makeToast("分享给好友")
    }
    
    @IBAction func logoutAction(_ sender: Any) {
    
        IGAlertVC.show(vc:self,title: LocalizedString("alert_tips"), message: LocalizedString("logout_tips"), commit: (LocalizedString("logout"),{
            
            UserManager.share.token = nil
            AppManager.share.loginPage()
            
        }), cancel: (LocalizedString("cancel"),nil))
    }
    private func setupHeadView() {
        
        let view = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 16))
        view.backgroundColor = UIColor.clear
        self.tableView.tableHeaderView = view
    }
    
    private func setupData() {
        
        self.title = LocalizedString("setting")
        
        dataSource = [["name":LocalizedString("speech_tans"),"icon":"翻译"],
                      ["name":LocalizedString("rate_search"),"icon":"汇率"]]
    }
}

extension AccountVC: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataSource.count
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "AccountCell") as! AccountCell
        
        let dict = dataSource[indexPath.row]
        cell.showLabel.text = dict["name"]
        cell.showIV.image = UIImage(named: dict["icon"]!)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let dict = dataSource[indexPath.row]
        let name = dict["name"]
        
        switch name {
        case LocalizedString("speech_tans"):
            
            let vc = TranslationVC()
            self.navigationController?.pushViewController(vc, animated: true)
        case LocalizedString("rate_search"):
            
            let vc = RateQueryVC()
            self.navigationController?.pushViewController(vc, animated: true)
        default:
            break
        }
    }
}

